package com.xx.account.service.impl;

import java.util.List;

import com.xx.account.domain.AccountRecord;
import com.xx.account.domain.AccountSendLeaflet;
import com.xx.account.mapper.AccountSendLeafletMapper;
import com.xx.common.exception.ServiceException;
import com.xx.common.utils.DateUtils;
import com.xx.common.utils.SecurityUtils;
import com.xx.common.utils.StringUtils;
import com.xx.common.utils.uuid.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xx.account.mapper.AccountWorkerMapper;
import com.xx.account.domain.AccountWorker;
import com.xx.account.service.IAccountWorkerService;

import javax.annotation.Resource;

/**
 * 投手Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-29
 */
@Service
public class AccountWorkerServiceImpl implements IAccountWorkerService
{
    @Resource
    private AccountWorkerMapper accountWorkerMapper;

    @Resource
    private AccountSendLeafletMapper accountSendLeafletMapper;

    /**
     * 查询投手
     *
     * @param id 投手主键
     * @return 投手
     */
    @Override
    public AccountWorker selectAccountWorkerById(String id)
    {
        return accountWorkerMapper.selectAccountWorkerById(id);
    }

    /**
     * 查询投手列表
     *
     * @param accountWorker 投手
     * @return 投手
     */
    @Override
    public List<AccountWorker> selectAccountWorkerList(AccountWorker accountWorker)
    {
        accountWorker.setCreateBy(SecurityUtils.getUserId().toString());
        return accountWorkerMapper.selectAccountWorkerList(accountWorker);
    }

    /**
     * 新增投手
     *
     * @param accountWorker 投手
     * @return 结果
     */
    @Override
    public int insertAccountWorker(AccountWorker accountWorker)
    {
        accountWorker.setId(UUID.randomUUID().toString().toString().replaceAll("-",""));
        accountWorker.setCount(0L);
        accountWorker.setCreateTime(DateUtils.getNowDate());
        accountWorker.setCreateBy(SecurityUtils.getUserId().toString());
        return accountWorkerMapper.insertAccountWorker(accountWorker);
    }

    /**
     * 修改投手
     *
     * @param accountWorker 投手
     * @return 结果
     */
    @Override
    public int updateAccountWorker(AccountWorker accountWorker)
    {
        return accountWorkerMapper.updateAccountWorker(accountWorker);
    }

    /**
     * 批量删除投手
     *
     * @param ids 需要删除的投手主键
     * @return 结果
     */
    @Override
    public int deleteAccountWorkerByIds(String[] ids)
    {

        // 删除前先查询选中的数据是否被引用，引用则不让删除
        StringBuilder sb = new StringBuilder();
        for (String workerId : ids) {
            List<AccountSendLeaflet> list = accountSendLeafletMapper.selectAllByWorkerId(workerId);
            if(!list.isEmpty()){
                sb.append("【").append(list.get(0).getWorkerName()).append("】");
            }
        }

        if(StringUtils.isNotEmpty(sb)){
            throw new ServiceException("投手名称：" + sb + "已经被引用，不允许删除！");
        }

        return accountWorkerMapper.deleteAccountWorkerByIds(ids);
    }

    /**
     * 删除投手信息
     *
     * @param id 投手主键
     * @return 结果
     */
    @Override
    public int deleteAccountWorkerById(String id)
    {
        return accountWorkerMapper.deleteAccountWorkerById(id);
    }
}
